package com.whh.permission.controller;

import com.whh.permission.po.*;
import com.whh.permission.service.IMenuService;
import com.whh.permission.util.JsonObject;
import com.whh.permission.util.R;
import com.whh.permission.util.TreeBuilder;
import com.whh.permission.util.TreeBuilder2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MenuController {
    @Resource
    private IMenuService menuServiceImpl;
    @Autowired
    private TreeBuilder treeBuilder;

    @Autowired
    private TreeBuilder2 treeBuilder2;
    @RequestMapping("menu/menuAll")
    @ResponseBody
    public JsonObject queryMenuAll(){
        JsonObject object = new JsonObject();
        List<Menu> menus = menuServiceImpl.queryMenuAll();
        object.setCode(200);
        object.setData(menus);
        object.setCount((long) menus.size());
        object.setMsg("ok");
        return object;
    }
    @RequestMapping("/menu")
    public String menuIndex(){
        return "pages/menu";
    }

    /**
     * 添加页面
     */
    @RequestMapping("/addMenu")
    public String addMenu(Model model,int type,int parentId){
        if (type==-1){
            type = 0;
            parentId = -1;
        }else{
            type = type+1;
        }
        model.addAttribute("type",type);
        model.addAttribute("parentId",parentId);
        return "pages/addMenu";
    }

    @RequestMapping("menu/addMenuSubmit")
    @ResponseBody
    public R addMenuSubmit(Menu menu){
        menu.setCreateTime(new Date());
        menuServiceImpl.addMenu(menu);
        return R.ok();
    }

    @RequestMapping("menu/deleteMenuByID")
    @ResponseBody
    public R deleteMenuById(Integer id){
        menuServiceImpl.deleteMenuById(id);
        return R.ok();
    }

    @RequestMapping("menu/queryMenuById")
    public String queryMenuById(Integer id,Model model){
        Menu menu = menuServiceImpl.queryMenuById(id);
        model.addAttribute("menu",menu);
        return "pages/updateMenu";
    }
    @RequestMapping("menu/updateMenuSubmit")
    @ResponseBody
    public R updateMenuSubmit(Menu menu){
        menuServiceImpl.updateMenuSubmit(menu);
        return R.ok();
    }

    @RequestMapping("menu/queryMenuTree")
    @ResponseBody
    public Object queryMenuTree(Integer id){
        //查询node数据
        List<Node> list = menuServiceImpl.queryMenuTree();

        //根据角色查询关联到的菜单
        List<RoleMenu> roleMenus=menuServiceImpl.queryMenuByRoleId(id);

        //渲染已经选择的内容
        for(RoleMenu rm:roleMenus){
            //获取当前对象的id
            int menuId=rm.getMenuId();
            for(Node node:list){
                if(node.getId()==menuId){
                    //判断是否有子节点
                    List<Menu> menusList=  menuServiceImpl.isChild(menuId);
                    if(menusList.size()==0){//如果没有子节点
                        node.setChecked(true);//设置为true 选中状态
                    }
                }
            }
        }



//        for (RoleMenu rm : roleMenus){
//            //获取当前对象的id'
//            int menuID = rm.getMenuId();
//            for (Node node:list){
//                if (node.getId()==menuID){
//                    node.setChecked(true);
//                }
//            }
//        }
        //组装树结构
        String result = treeBuilder.buildTree(list);
        return result;
    }


    /**
     * 根据用户id查询导航树
     */
    @RequestMapping("menuTree")
    @ResponseBody
    public Object MenuNavTree(HttpSession session){
        //获取登录用户对象
        User user= (User) session.getAttribute("user");
        //根据id获取菜单
        List<NavNode> obj=menuServiceImpl.queryNavNodeListTree(user.getId());

        //拼接数据
        Map map=new HashMap<>();
        //设置home
        Map home=new HashMap<>();
        home.put("title","系统管理");
        home.put("href","pages/welcome.html");
        map.put("homeInfo",home);

        //logoInfo
        Map logo=new HashMap<>();
        logo.put("title","后台管理");
        logo.put("image","images/logo.png");
        logo.put("href","");
        map.put("logoInfo",logo);

        //设置导航数据
        //1 转成树结构
        obj= treeBuilder2.buildTree(obj);
        map.put("menuInfo",obj);
        System.out.println(map);

        return map;
    }


}
